package com.group01.community.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group01.community.entity.ZyOwner;
import com.group01.community.entity.vo.ZyOwnerVO;
import com.group01.community.mapper.ZyOwnerMapper;
import com.group01.community.service.ZyOwnerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group01.community.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


/**
 * <p>
 * 业主  服务实现类
 * </p>
 *
 * @author guoyuan
 * @since 2025-05-01
 */
@Service
public class ZyOwnerServiceImpl extends ServiceImpl<ZyOwnerMapper, ZyOwner> implements ZyOwnerService {

    @Autowired
    private ZyOwnerMapper zyOwnerMapper;


    @Override
    public IPage<ZyOwnerVO> sybase(Long current, Long size, ZyOwnerVO zyOwnerVO) {
        Page<ZyOwnerVO> page = new Page<>(current, size);
        page.setOptimizeCountSql(false);
        return zyOwnerMapper.sybase(page,zyOwnerVO);
    }

    @Override
    public IPage<ZyOwnerVO> queryOwnerBaseInfo(Long current, Long size, ZyOwnerVO zyOwnerVO) {
        Page<ZyOwnerVO> page = new Page<>(current, size);
        page.setOptimizeCountSql(false);
        return zyOwnerMapper.queryOwnerBaseInfo(page,zyOwnerVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveZyOwner(ZyOwner zyOwner) {
        zyOwner.setOwnerId(UUIDUtils.getId());
        String year = zyOwner.getOwnerIdCard().substring(6, 10);
        String month = zyOwner.getOwnerIdCard().substring(10, 12);
        String day = zyOwner.getOwnerIdCard().substring(12, 14);
        String birthDay = year + "-" + month + "-" + day;
        zyOwner.setOwnerBirthday(LocalDate.parse(birthDay, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        zyOwner.setOwnerType("业主");
        zyOwner.setOwnerStatus("Enable");
        zyOwner.setCreateTime(LocalDateTime.now());

        int res = zyOwnerMapper.insertZyOwner(zyOwner);
        return res > 0;
    }
}
